<?php

/**
 * 游戏
 */
class Api_Game extends PhalApi_Api
{
	
	public function getRules()
	{
		return array(
			'settleGame'      => array(
				'uid'    => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'gameid' => array('name' => 'gameid', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '游戏ID'),
			),
			'checkGame'       => array(
				'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
				'stream'  => array('name' => 'stream', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '流名'),
			),
			'getGameRecord'   => array(
				'action' => array('name' => 'action', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '游戏类别'),
				'stream' => array('name' => 'stream', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '流名'),
			),
			/* 智勇三张 */
			'Jinhua'          => array(
				'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
				'stream'  => array('name' => 'stream', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '流名'),
				'token'   => array('name' => 'token', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => 'token'),
			),
			'endGame'         => array(
				'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
				'gameid'  => array('name' => 'gameid', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '游戏ID'),
				'token'   => array('name' => 'token', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => 'token'),
				'type'    => array('name' => 'type', 'type' => 'string', 'min' => 0, 'require' => true, 'desc' => '结束类型，1为正常结束，2为主播关闭，3为意外断开'),
				'ifset'   => array('name' => 'ifset', 'type' => 'int', 'default' => 0, 'desc' => '是否设置'),
			),
			'JinhuaBet'       => array(
				'uid'    => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'gameid' => array('name' => 'gameid', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '游戏ID'),
				'token'  => array('name' => 'token', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => 'token'),
				'coin'   => array('name' => 'coin', 'type' => 'string', 'min' => 0, 'require' => true, 'desc' => '下注金额'),
				'grade'  => array('name' => 'grade', 'type' => 'string', 'min' => 0, 'require' => true, 'desc' => '下注位置，1,2,3'),
			),
			/* 转盘 */
			'Dial'            => array(
				'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
				'stream'  => array('name' => 'stream', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '流名'),
				'token'   => array('name' => 'token', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => 'token'),
			),
			'Dial_end'        => array(
				'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
				'gameid'  => array('name' => 'gameid', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '游戏ID'),
				'token'   => array('name' => 'token', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => 'token'),
				'type'    => array('name' => 'type', 'type' => 'string', 'min' => 0, 'require' => true, 'desc' => '结束类型，1为正常结束，2为主播关闭，3为意外断开'),
				'ifset'   => array('name' => 'ifset', 'type' => 'int', 'default' => 0, 'desc' => '是否设置'),
			),
			'Dial_Bet'        => array(
				'uid'    => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'gameid' => array('name' => 'gameid', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '游戏ID'),
				'token'  => array('name' => 'token', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => 'token'),
				'coin'   => array('name' => 'coin', 'type' => 'string', 'min' => 0, 'require' => true, 'desc' => '下注金额'),
				'grade'  => array('name' => 'grade', 'type' => 'string', 'min' => 0, 'require' => true, 'desc' => '下注位置，1,2,3,4,5,6'),
			),
			/* 庄家 */
			'getBankerProfit' => array(
				'bankerid' => array('name' => 'bankerid', 'type' => 'int', 'require' => true, 'desc' => '庄家ID'),
				/* 'action' => array('name' => 'action', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '游戏类别'), */
				'stream'   => array('name' => 'stream', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '流名'),
			),
			'getBanker'       => array(
				'stream' => array('name' => 'stream', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '流名'),
			),
			'setBanker'       => array(
				'uid'     => array('name' => 'uid', 'type' => 'int', 'require' => true, 'desc' => '用户ID'),
				'token'   => array('name' => 'token', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '用户token'),
				'stream'  => array('name' => 'stream', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '流名'),
				'deposit' => array('name' => 'deposit', 'type' => 'string', 'desc' => '押金'),
			),
			'quietBanker'     => array(
				'uid'    => array('name' => 'uid', 'type' => 'int', 'require' => true, 'desc' => '用户ID'),
				'stream' => array('name' => 'stream', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '流名'),
			),
		);
	}
	
	/**
	 * 游戏结算
	 * @desc 用于游戏结算
	 * @return int code 操作码，0表示成功
	 * @return array info[0]
	 * @return string info[0].gamecoin 用户中奖金额
	 * @return string info[0].coin 用户余额
	 * @return string info[0].banker_profit 庄家收益
	 * @return string info[0].isshow 是否显示自动下庄通知，0表示不显示，1表示显示
	 * @return string msg 提示信息
	 */
	public function settleGame()
	{
		$uid    = checkNull($this->uid);
		$gameid = checkNull($this->gameid);
		$domain = new Domain_Game();
		
		$settleGame = $domain->settleGame($uid, $gameid);
		if($settleGame == 1000){
			return $this->fail('游戏信息不存在', 1000);
		}
		
		return $this->success(['0' => $settleGame]);
	}
	
	/**
	 * 检测游戏状态
	 * @desc 用于检测游戏状态
	 * @return int code 操作码，0表示成功
	 * @return array info[0]
	 * @return string info[0].gamecoin 用户中奖金额
	 * @return string info[0].coin 用户余额
	 * @return string msg 提示信息
	 */
	public function checkGame()
	{
		$liveuid = checkNull($this->liveuid);
		$stream  = checkNull($this->stream);
		
		$domain = new Domain_Game();
		$info   = $domain->checkGame($liveuid, $stream);
		
		return $this->success();
	}
	
	/**
	 * 智勇三张游戏开启
	 * @desc 用于智勇三张游戏开启
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string info[0].time 倒计时间
	 * @return string info[0].Jinhuatoken 游戏token
	 * @return string info[0].gameid 游戏记录ID
	 * @return string msg 提示信息
	 */
	public function Jinhua()
	{
		$liveuid = checkNull($this->liveuid);
		$stream  = checkNull($this->stream);
		$token   = checkNull($this->token);
		if($liveuid < 1 || $token == '' || $stream == ''){
			return $this->fail('信息错误', 1001);
		}
		
		$checkToken = checkToken($liveuid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		$domain = new Domain_Game();
		$info   = $this->Jinhua_info();
		$time   = time();
		if($info[0][3] == "1"){
			$result = "1";
		} elseif($info[1][3] == "1"){
			$result = "2";
		} else{
			$result = "3";
		}
		
		$record = $domain->record($liveuid, $stream, "1", $time, $result, 0, '',$info);
		if($record == 1000){
			$game = DI()->notorm->game->select("*")->where('stream=? and state=0', $stream)->fetchOne();
			
			return $this->fail('本轮游戏还未结束', 1000, $game);
		}
		if($record == 1001){
			return $this->fail('游戏开启失败', 1001);
		}
		
		$gameToken = $stream."_1_".$time;
		DI()->redis->set($gameToken."_Game", json_encode($info));
		
		$Jinhua['time']   = "30";
		$Jinhua['token']  = $gameToken;
		$Jinhua['gameid'] = $record['id'];
		
		return $this->success(['0' => $Jinhua]);
	}
	
	// 金花测试 10w次
	// https://s1.scbjxs.com/appapi/index.php?service=Game.JinhuaTest
	public function JinhuaTest()
	{
		$all = ['num' => 0, '1' => 0, '2' => 0, '3' => 0,];
		$pai = [];
		for($num = 0; $num < 2; $num++){
			$info = $this->Jinhua_info();
			if($info[0][3] == "1"){
				$result = "1";
				$all[1]++;
			} elseif($info[1][3] == "1"){
				$result = "2";
				$all[2]++;
			} else{
				$result = "3";
				$all[3]++;
			}
			$all['num']++;
			//$this->saveLog(json_encode($info, JSON_UNESCAPED_UNICODE), '牌面信息');
			$this->saveLog('执行次数：'.$all['num'].'  获胜位置：'.$result);
		}
		$this->saveLog(json_encode($all, JSON_UNESCAPED_UNICODE), '获胜次数');
		
		return $this->success();
	}
	
	/**
	 * 智勇三张游戏关闭
	 * @desc 用于智勇三张游戏关闭
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string msg 提示信息
	 */
	public function endGame()
	{
		$liveuid = checkNull($this->liveuid);
		$gameid  = checkNull($this->gameid);
		$ifset   = checkNull($this->ifset);
		$token   = checkNull($this->token);
		$type    = checkNull($this->type);
		
		$checkToken = checkToken($liveuid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		$domain = new Domain_Game();
		$info   = $domain->endGame($liveuid, $gameid, $type, $ifset);
		if($info == 1000){
			return $this->fail('该游戏已经被关闭', 1000);
		}
		
		return $this->success($info);
	}
	
	/**
	 * 智勇三张游戏下注
	 * @desc 用于智勇三张游戏下注
	 * @return int code 操作码，0表示成功
	 * @return array info[0]
	 * @return string info[0].uid 用户ID
	 * @return string info[0].coin 用户余额
	 * @return string info[0].level 用户等级
	 * @return string msg 提示信息
	 */
	public function JinhuaBet()
	{
		$uid    = checkNull($this->uid);
		$gameid = checkNull($this->gameid);
		$token  = checkNull($this->token);
		$coin   = checkNull($this->coin);
		$grade  = checkNull($this->grade);
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		$domain = new Domain_Game();
		$info   = $domain->gameBet($uid, $gameid, $coin, "1", $grade);
		
		//file_put_contents('./gameBet.txt',date('Y-m-d H:i:s').' 提交参数信息 info:'.json_encode($info)."\r\n",FILE_APPEND);
		if($info == 1000){
			return $this->fail('你的余额不足，无法下注!', 1000);
		} elseif($info == 1001){
			return $this->fail('本轮游戏已经结束!', 1001);
		} elseif($info == 1002){
			return $this->fail('下注失败!', 1002);
		} elseif($info == 1003){
			return $this->fail('下注金额已达上限!', 1003);
		}
		
		$gameToken = $info['stream']."_1_".$info['gametime']."_Game";
		$BetRedis  = DI()->redis->Get($gameToken);
		$BetRedis  = json_decode($BetRedis, 1);
		$grade     = $grade - 1;
		
		$BetRedis[$grade][5] = (string)($coin + $BetRedis[$grade][5]);
		
		DI()->redis->set($gameToken, json_encode($BetRedis));
		$JinhuaBet['uid']  = (string)$uid;
		$JinhuaBet['coin'] = $info['coin'];
		
		$userinfo           = getUserInfo($uid);
		$JinhuaBet['level'] = $userinfo['level'];
		
		/* $rs['info']['gameid']=$info['gameid']; */
		
		//file_put_contents('./gameBet.txt',date('Y-m-d H:i:s').' 提交参数信息 rs:'.json_encode($rs)."\r\n",FILE_APPEND);
		return $this->success([0 => $JinhuaBet]);
	}
	
	/**
	 * 转盘游戏开启
	 * @desc 用于转盘游戏开启
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string info[0].time 倒计时间
	 * @return string info[0].token 游戏token
	 * @return string info[0].gameid 游戏记录ID
	 * @return string msg 提示信息
	 */
	public function Dial()
	{
		$liveuid = checkNull($this->liveuid);
		$stream  = checkNull($this->stream);
		$token   = checkNull($this->token);
		if($liveuid < 1 || $token == '' || $stream == ''){
			return $this->fail('信息错误!', 1001);
		}
		
		$checkToken = checkToken($liveuid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		$domain = new Domain_Game();
		$result = rand(1, 4);
		$time   = time();
		$record = $domain->record($liveuid, $stream, "3", $time, $result);
		if($record == 1000){
			return $this->fail('本轮游戏还未结束!', 1000);
		}
		if($record == 1001){
			return $this->fail('游戏开启失败!', 1001);
		}
		
		$gameToken = $stream."_3_".$time;
		$info      = array($result, '0', '0', '0', '0');
		DI()->redis->set($gameToken."_Game", json_encode($info));
		
		$Taurus['time']   = "30";
		$Taurus['token']  = $gameToken;
		$Taurus['gameid'] = $record['id'];
		
		return $this->success([0 => $Taurus]);
	}
	
	/**
	 * 转盘游戏关闭
	 * @desc 用于转盘游戏关闭
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string msg 提示信息
	 */
	public function Dial_end()
	{
		$liveuid = checkNull($this->liveuid);
		$gameid  = checkNull($this->gameid);
		$ifset   = checkNull($this->ifset);
		$token   = checkNull($this->token);
		$type    = checkNull($this->type);
		
		$checkToken = checkToken($liveuid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		$domain = new Domain_Game();
		$info   = $domain->endGame($liveuid, $gameid, $type, $ifset);
		if($info == 1000){
			//return $this->fail('该游戏已经被关闭!', 1000);
			return $this->success();
		}
		
		return $this->success($info);
	}
	
	/**
	 * 转盘游戏下注
	 * @desc 用于转盘游戏下注
	 * @return int code 操作码，0表示成功
	 * @return array info[0]
	 * @return string info[0].uid 用户ID
	 * @return string info[0].coin 用户余额
	 * @return string info[0].level 用户等级
	 * @return string msg 提示信息
	 */
	public function Dial_Bet()
	{
		//file_put_contents('./111111.txt',date('Y-m-d H:i:s')." 进入接口Dial_Bet：\r\n",FILE_APPEND);
		$uid    = checkNull($this->uid);
		$gameid = checkNull($this->gameid);
		$token  = checkNull($this->token);
		$coin   = checkNull($this->coin);
		$grade  = checkNull($this->grade);
		
		$checkToken = checkToken($uid, $token);
		//file_put_contents('./111111.txt',date('Y-m-d H:i:s')." checkToken:\r\n",FILE_APPEND);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		//file_put_contents('./111111.txt',date('Y-m-d H:i:s')." 请求model:\r\n",FILE_APPEND);
		$domain = new Domain_Game();
		$info   = $domain->gameBet($uid, $gameid, $coin, "3", $grade);
		//file_put_contents('./111111.txt',date('Y-m-d H:i:s')." 返回info".json_encode($info).":\r\n",FILE_APPEND);
		if($info == 1000){
			return $this->fail('你的余额不足，无法下注!', 1000);
		}
		if($info == 1001){
			return $this->fail('本轮游戏已经结束!', 1001);
		}
		if($info == 1002){
			return $this->fail('下注失败!', 1002);
		}
		if($info == 1003){
			return $this->fail('下注金额已达上限!', 1003);
		}
		
		$gameToken = $info['stream']."_3_".$info['gametime']."_Game";
		$BetRedis  = DI()->redis->Get($gameToken);
		//file_put_contents('./121212.txt',date('Y-m-d H:i:s').' 获取redis数据：'.$BetRedis."\r\n",FILE_APPEND);
		$BetRedis         = json_decode($BetRedis, 1);
		$grade            = $grade;
		$BetRedis[$grade] = (string)($coin + $BetRedis[$grade]);
		
		//file_put_contents('./121212.txt',date('Y-m-d H:i:s').' 用户下注:'.$grade.',金额为：'.$coin.'下注后的数据：'.json_encode($BetRedis)."\r\n",FILE_APPEND);
		
		DI()->redis->set($gameToken, json_encode($BetRedis));
		$TaurusBet['uid']  = $info['uid'];
		$TaurusBet['coin'] = $info['coin'];
		
		$userinfo           = getUserInfo($uid);
		$TaurusBet['level'] = $userinfo['level'];
		
		return $this->success([0 => $TaurusBet]);
	}
	/* 转盘 end */
	
	
	/* 智勇三张牌面处理 */
	/**
	 * 智勇三张牌面
	 * @desc 用于获取智勇三张牌面
	 * @return array info[][0] 第一张牌
	 * @return array info[][1] 第二张牌
	 * @return array info[][2] 第三张牌
	 * @return array info[][3] 是否最大
	 * @return array info[][4] 牌组类型名称
	 * @return array info[][5]
	 * @return array info[][6] 牌组类型
	 * @return string msg 提示信息
	 */
	protected function Jinhua_info()
	{
		/* 花色	4表示黑桃 3表示红桃 2表示方片  1表示梅花 */
		/* 牌面 格式 花色-数字 14代表1(PS：请叫它A (jian))*/
		$cards = array(
			'1-14',
			'1-2',
			'1-3',
			'1-4',
			'1-5',
			'1-6',
			'1-7',
			'1-8',
			'1-9',
			'1-10',
			'1-11',
			'1-12',
			'1-13',
			'2-14',
			'2-2',
			'2-3',
			'2-4',
			'2-5',
			'2-6',
			'2-7',
			'2-8',
			'2-9',
			'2-10',
			'2-11',
			'2-12',
			'2-13',
			'3-14',
			'3-2',
			'3-3',
			'3-4',
			'3-5',
			'3-6',
			'3-7',
			'3-8',
			'3-9',
			'3-10',
			'3-11',
			'3-12',
			'3-13',
			'4-14',
			'4-2',
			'4-3',
			'4-4',
			'4-5',
			'4-6',
			'4-7',
			'4-8',
			'4-9',
			'4-10',
			'4-11',
			'4-12',
			'4-13',
		);
		shuffle($cards);
		
		// 第一种方式获取拍
		/*$card1 = array_slice($cards, 0, 3);
		$card2 = array_slice($cards, 3, 3);
		$card3 = array_slice($cards, 6, 3);
		$this->saveLog($card1,'第一个人牌');
		$this->saveLog($card2,'第一个人牌');
		$this->saveLog($card3,'第一个人牌');*/
		
		// 第二种方式获取拍
		$card1 = $card2 = $card3 = [];
		foreach($cards as $key => $item){
			if($key >= 9){
				break;
			}
			if($key % 3 == 0){
				$card1[] = $item;
			}
			if($key % 3 == 1){
				$card2[] = $item;
			}
			if($key % 3 == 2){
				$card3[] = $item;
			}
		}
		//$this->saveLog(json_encode($card1), '第一个人的牌');
		//$this->saveLog(json_encode($card2), '第一个人的牌');
		//$this->saveLog(json_encode($card3), '第一个人的牌');
		
		
		$Card_one   = $this->Jinhua_Card($card1);
		$Card_two   = $this->Jinhua_Card($card2);
		$Card_three = $this->Jinhua_Card($card3);
		$compare    = $this->Jinhua_compare($Card_one, $Card_two, $Card_three);
		
		$card1[] = (string)$compare['one_bright'];
		$card1[] = $Card_one['name'];
		$card1[] = "0";
		$card1[] = (string)$Card_one['card'];
		$card2[] = (string)$compare['two_bright'];
		$card2[] = $Card_two['name'];
		$card2[] = "0";
		$card2[] = (string)$Card_two['card'];
		$card3[] = (string)$compare['three_bright'];
		$card3[] = $Card_three['name'];
		$card3[] = "0";
		$card3[] = (string)$Card_three['card'];
		
		$rs[] = $card1;
		$rs[] = $card2;
		$rs[] = $card3;
		
		//$this->saveLog(json_encode($rs, JSON_UNESCAPED_UNICODE), '牌面信息');
		
		return $rs;
	}
	
	/*分析牌面 类型*/
	protected function Jinhua_Card($deck)
	{
		$deck_rs = array();
		foreach($deck as $k => $v){
			$carde                = explode('-', $v);
			$deck_rs[$k]['color'] = $carde[0];
			$deck_rs[$k]['brand'] = $carde[1];
			$order[$k]            = $carde[1];
			array_multisort($order, SORT_DESC, $deck_rs);
		}
		
		/* 	return $deck_rs; */
		$brand_one   = $deck_rs[0]['brand'];
		$brand_two   = $deck_rs[1]['brand'];
		$brand_three = $deck_rs[2]['brand'];
		$color_one   = $deck_rs[0]['color'];
		$color_two   = $deck_rs[1]['color'];
		$color_three = $deck_rs[2]['color'];
		
		$rs              = array();
		$rs['val_one']   = $brand_one;
		$rs['val_two']   = $brand_two;
		$rs['val_three'] = $brand_three;
		$rs['color']     = 0;
		
		$along  = 0;
		$people = array(array(14, 3, 2), array(14, 2, 3), array(3, 2, 14), array(3, 14, 2), array(2, 14, 3), array(2, 3, 14));
		if(in_array(array($brand_one, $brand_two, $brand_three), $people)){
			$along = 1;
		}
		
		if($brand_one == $brand_two && $brand_two == $brand_three){    //豹子
			$rs['card'] = 6;
			$rs['name'] = "豹子";
		} elseif($color_one == $color_two && $color_two == $color_three && (($brand_one - 2) == $brand_three || $along == 1)){//同花顺
			$rs['color'] = $color_three;
			$rs['card']  = 5;
			$rs['name']  = "同花顺";
		} elseif($color_one == $color_two && $color_two == $color_three){    //同花
			$rs['color'] = $color_three;
			$rs['card']  = 4;
			$rs['name']  = "同花";
		} elseif($brand_one == $brand_two || $brand_two == $brand_three || $brand_one == $brand_three){//对子
			$rs['card'] = 2;
			$rs['name'] = "对子";
			if($brand_one == $brand_two)//1==2
			{
				$rs['val_one']   = $brand_two;
				$rs['val_three'] = $brand_three;
				$rs['color']     = $color_three;
			} elseif($brand_three == $brand_two){//2==3
				$rs['val_one']   = $brand_three;
				$rs['val_three'] = $brand_one;
				$rs['color']     = $color_one;
			} else{//1==3
				$rs['val_one']   = $brand_one;
				$rs['val_three'] = $brand_two;
				$rs['color']     = $color_two;
			}
		} elseif((($brand_one - 2) == $brand_three || $along == 1) && ($brand_one != $brand_two || $brand_two != $brand_three || $brand_one != $brand_three)){//顺子
			$rs['color'] = $color_one;
			$rs['card']  = 3;
			$rs['name']  = "顺子";
		} else{//单张
			$rs['color'] = $color_one;
			$rs['card']  = 1;
			$rs['name']  = "单牌";
		}
		
		return $rs;
	}
	
	/**
	 * 判断三副牌的类型大小 找出类型最大的牌
	 * val_one为三张牌中最大的那一张
	 * $rs['one_bright'] 是否为最大 0为否 1为是
	 * $null设置一个空数组 当只有2副牌 是相同是 传null 这个数组替代
	 **/
	protected function Jinhua_compare($one, $two, $three)
	{
		$rs   = array();
		$null = array("val_one" => '0', "val_two" => '0', "val_three" => '0', "color" => '0', "card" => '0',);
		
		$rs['one_bright']   = 0;
		$rs['two_bright']   = 0;
		$rs['three_bright'] = 0;
		if($one['card'] == $two['card'] && $two['card'] == $three['card']){//三张牌的类型一致
			$belongTo = $this->Jinhua_belongTo($one['card'], $one, $two, $three, 0);
			if($belongTo == "2"){
				$rs['two_bright'] = 1;
			} elseif($belongTo == "1"){
				$rs['one_bright'] = 1;
			} else{
				$rs['three_bright'] = 1;
			}
		} elseif($one['card'] == $two['card']){//一号牌与二号牌的类型一致
			if($one['card'] < $three['card']){
				$rs['three_bright'] = 1;
			} else{
				$belongTo = $this->Jinhua_belongTo($one['card'], $one, $two, $null, 1);
				if($belongTo == 2){
					$rs['two_bright'] = 1;
				} else{
					$rs['one_bright'] = 1;
				}
			}
		} elseif($one['card'] == $three['card']){//一号牌与三号牌的类型一致
			if($one['card'] < $two['card']){
				$rs['two_bright'] = 1;
			} else{
				$belongTo = $this->Jinhua_belongTo($one['card'], $one, $null, $three, 1);
				if($belongTo == 3){
					$rs['three_bright'] = 1;
				} else{
					$rs['one_bright'] = 1;
				}
			}
		} elseif($two['card'] == $three['card']){//二号牌与三号牌的类型一致
			if($two['card'] < $one['card']){
				$rs['one_bright'] = 1;
			} else{
				$belongTo = $this->Jinhua_belongTo($one['card'], $null, $two, $three, 1);
				if($belongTo == 2){
					$rs['two_bright'] = 1;
				} else{
					$rs['three_bright'] = 1;
				}
			}
		} else{//三种牌的类型都不一致
			if($one['card'] > $two['card']){
				if($one['card'] > $three['card']){
					$rs['one_bright'] = 1;
				} else{
					$rs['three_bright'] = 1;
				}
			} else{
				if($two['card'] > $three['card']){
					$rs['two_bright'] = 1;
				} else{
					$rs['three_bright'] = 1;
				}
			}
		}
		
		return $rs;
	}
	
	/**
	 * 判断相同类型的牌
	 * val_one 为三张牌中最大的 那一张
	 * type 0代表三副牌的类型一致 1代表只有两副牌的类型一致
	 **/
	protected function Jinhua_belongTo($card, $one, $two, $three, $type)
	{
		$rs = array();
		if($card == 6){//三副牌都是豹子比较
			$rs = $this->leopard_than($one, $two, $three);
		} elseif($card == 5){//三副牌都是同花顺比较
			$rs = $this->flush_than($one, $two, $three);
		} elseif($card == 4){//同花
			$rs = $this->flower_than($one, $two, $three);
		} elseif($card == 3){//顺子
			$rs = $this->along_than($one, $two, $three);
		} elseif($card == 2){//对子
			$rs = $this->sub_than($one, $two, $three);
		} else{//单张
			$rs = $this->single_than($one, $two, $three);
		}
		
		return $rs;
	}
	
	/**
	 * 豹子比较
	 **/
	protected function leopard_than($one, $two, $three)
	{
		if($one['val_one'] > $two['val_one']){
			if($one['val_one'] > $three['val_one']){
				return 1;
			} else{
				return 3;
			}
		} else{
			if($two['val_one'] > $three['val_one']){
				return 2;
			} else{
				return 3;
			}
		}
	}
	
	/**
	 * 同花顺比较
	 **/
	protected function flush_than($one, $two, $three)
	{
		if($two['val_one'] == $three['val_one'] && $one['val_one'] == $three['val_one']){//三副牌的牌面数字大小一致
			if($one['color'] > $two['color']){
				if($one['color'] > $three['color']){
					return 1;
				} else{
					return 3;
				}
			} else{
				if($two['color'] > $three['color']){
					return 2;
				} else{
					return 3;
				}
			}
		} elseif($two['val_one'] == $one['val_one']){//一号牌和二号牌的牌面大小一致
			if($two['val_one'] > $three['val_one']){
				if($two['color'] > $one['color']){
					return 2;
				} else{
					return 1;
				}
			} else{
				return 3;
			}
		} elseif($one['val_one'] == $three['val_one']){//一号牌和三号牌的牌面大小一致
			if($one['val_one'] > $two['val_one']){
				if($one['color'] > $three['color']){
					return 1;
				} else{
					return 3;
				}
			} else{
				return 2;
			}
		} elseif($two['val_one'] == $three['val_one']){//二号牌和三号牌的牌面大小一致
			if($two['val_one'] > $one['val_one']){
				if($two['color'] > $three['color']){
					return 2;
				} else{
					return 3;
				}
			} else{
				return 1;
			}
		} else{//三副牌的牌面大小均不一致
			if($one['val_one'] > $two['val_one']){
				if($one['val_one'] > $three['val_one']){
					return 1;
				} else{
					return 3;
				}
			} else{
				if($two['val_one'] > $three['val_one']){
					return 2;
				} else{
					return 3;
				}
			}
		}
	}
	
	/**
	 * 同花比较
	 **/
	protected function flower_than($one, $two, $three)
	{
		if($two['val_one'] == $three['val_one'] && $one['val_one'] == $three['val_one']){//三副牌的第一张牌的牌面一致
			if($two['val_two'] == $three['val_two'] && $one['val_two'] == $three['val_two']){//三副牌的第二张牌的牌面一致
				//三副牌的第三张牌的牌面一致(一致用 花色比较  不一致比较大小)
				if($two['val_three'] == $three['val_three'] && $one['val_three'] == $three['val_three']){
					$common = $this->than($one['color'], $two['color'], $three['color']);
					
					return $common;
				} elseif($two['val_three'] == $one['val_three']){//一号牌和二号牌的第三张牌牌面一样
					if($two['val_three'] > $three['val_three']){
						if($two['color'] > $one['color']){
							return 2;
						} else{
							return 1;
						}
					} else{
						return 3;
					}
				} elseif($three['val_three'] == $one['val_three']){//一号牌和三号牌的第三张牌牌面一样
					if($one['val_three'] > $two['val_three']){
						if($three['color'] > $one['color']){
							return 3;
						} else{
							return 1;
						}
					} else{
						return 2;
					}
				} elseif($two['val_three'] == $three['val_three']){//二号牌和三号牌的第三张牌牌面一样
					if($two['val_three'] > $one['val_three']){
						if($two['color'] > $three['color']){
							return 3;
						} else{
							return 2;
						}
					} else{
						return 1;
					}
				} else{//三副牌的第三张拍的牌面均不一致
					$common = $this->than($one['val_three'], $two['val_three'], $three['val_three']);
					
					return $common;
				}
			} elseif($two['val_two'] == $one['val_two']){//一号牌和二号牌的第二张牌牌面一样
				if($two['val_two'] > $three['val_two']){
					if($two['val_three'] == $one['val_three']){
						if($two['color'] > $one['color']){
							return 2;
						} else{
							return 1;
						}
					} else{
						if($two['val_three'] > $one['val_three']){
							return 2;
						} else{
							return 1;
						}
					}
				} else{
					return 3;
				}
			} elseif($three['val_two'] == $one['val_two']){//一号牌和三号牌的第二张牌牌面一样
				if($three['val_two'] > $two['val_two']){
					if($three['val_three'] == $one['val_three']){
						if($three['color'] > $one['color']){
							return 3;
						} else{
							return 1;
						}
					} else{
						if($three['val_three'] > $one['val_three']){
							return 3;
						} else{
							return 1;
						}
					}
				} else{
					return 2;
				}
			} elseif($three['val_two'] == $two['val_two']){//二号牌和三号牌的第二张牌牌面一样
				if($three['val_two'] > $one['val_two']){
					if($three['val_three'] == $two['val_three']){
						if($three['color'] > $two['color']){
							return 3;
						} else{
							return 2;
						}
					} else{
						if($three['val_three'] > $two['val_three']){
							return 3;
						} else{
							return 2;
						}
					}
				} else{
					return 1;
				}
			} else{
				
			}
		} elseif($two['val_one'] == $one['val_one']){//一号牌和二号牌的第一张牌牌面一样
			if($two['val_one'] > $three['val_one']){
				if($two['val_two'] == $one['val_two']){
					if($two['val_three'] == $one['val_three']){
						if($two['color'] > $one['color']){
							return 2;
						} else{
							return 1;
						}
					} else{
						if($two['val_three'] > $one['val_three']){
							return 2;
						} else{
							return 1;
						}
					}
				} else{
					if($two['val_two'] > $one['val_two']){
						return 2;
					} else{
						return 1;
					}
				}
			} else{
				return 3;
			}
		} elseif($three['val_one'] == $one['val_one']){//一号牌和三号牌的第一张牌牌面一样
			if($two['val_one'] > $one['val_one']){
				if($three['val_two'] == $one['val_two']){
					if($three['val_three'] == $one['val_three']){
						if($three['color'] > $one['color']){
							return 3;
						} else{
							return 1;
						}
					} else{
						if($three['val_three'] > $one['val_three']){
							return 3;
						} else{
							return 1;
						}
					}
				} else{
					if($three['val_two'] > $one['val_two']){
						return 3;
					} else{
						return 1;
					}
				}
			} else{
				return 2;
			}
		} elseif($three['val_one'] == $two['val_one']){//二号牌和三号牌的第一张牌牌面一样
			if($two['val_one'] > $one['val_one']){
				if($three['val_two'] == $two['val_two']){
					if($three['val_three'] == $two['val_three']){
						if($three['color'] > $two['color']){
							return 3;
						} else{
							return 2;
						}
					} else{
						if($three['val_three'] > $two['val_three']){
							return 3;
						} else{
							return 2;
						}
					}
				} else{
					if($three['val_two'] > $two['val_two']){
						return 3;
					} else{
						return 2;
					}
				}
			} else{
				return 1;
			}
		} else{
			$common = $this->than($one['val_one'], $two['val_one'], $three['val_one']);
			
			return $common;
		}
	}
	
	protected function than($one, $two, $three)
	{
		if($one > $two){
			if($one > $three){
				return 1;
			} else{
				return 3;
			}
		} else{
			if($two > $three){
				return 2;
			} else{
				return 3;
			}
		}
	}
	
	/**
	 * 顺子比较
	 * 流程 一次比较最大 如果三张牌相同 则比较嘴的牌的花色
	 **/
	protected function along_than($one, $two, $three)
	{
		if($two['val_one'] == $three['val_one'] && $one['val_one'] == $three['val_one']){
			$common = $this->than($one['color'], $two['color'], $three['color']);
			
			return $common;
		} elseif($one['val_one'] == $two['val_one']){//一号牌和二号牌牌面一直
			if($one['val_one'] > $three['val_one']){
				$common = $this->than($one['color'], $two['color'], 0);
				
				return $common;
			} else{
				return 3;
			}
		} elseif($one['val_one'] == $three['val_one']){//一号牌和三号牌牌面一直
			if($one['val_one'] > $two['val_one']){
				$common = $this->than($one['color'], 0, $two['color']);
				
				return $common;
			} else{
				return 2;
			}
		} elseif($three['val_one'] == $two['val_one']){//二号牌和三号牌牌面一直
			if($two['val_one'] > $one['val_one']){
				$common = $this->than(0, $two['color'], $two['color']);
				
				return $common;
			} else{
				return 1;
			}
		} else{
			$common = $this->than($one['val_one'], $two['val_one'], $three['val_one']);
			
			return $common;
		}
	}
	
	
	/*对子比较*/
	protected function sub_than($one, $two, $three)
	{
		if($one['val_one'] == $two['val_one']){//一号牌和二号牌牌面一致
			if($one['val_one'] > $three['val_one']){
				if($one['val_three'] == $two['val_three']){
					if($one['color'] > $two['color']){
						return 1;
					} else{
						return 2;
					}
				} else{
					if($one['val_three'] > $two['val_three']){
						return 1;
					} else{
						return 2;
					}
				}
			} else{
				return 3;
			}
		} elseif($one['val_one'] == $three['val_one']){//一号牌和三号牌牌面一致
			if($one['val_one'] > $two['val_one']){
				if($one['val_three'] == $three['val_three']){
					if($one['color'] > $three['color']){
						return 1;
					} else{
						return 3;
					}
				} else{
					if($one['val_three'] > $three['val_three']){
						return 1;
					} else{
						return 3;
					}
				}
			} else{
				return 2;
			}
		} elseif($two['val_one'] == $three['val_one']){//二号牌和三号牌牌面一致
			if($two['val_one'] > $one['val_one']){
				if($two['val_three'] == $three['val_three']){
					if($two['color'] > $three['color']){
						return 2;
					} else{
						return 3;
					}
				} else{
					if($two['val_three'] > $three['val_three']){
						return 2;
					} else{
						return 3;
					}
				}
			} else{
				return 1;
			}
		} else{
			$common = $this->than($one['val_one'], $two['val_one'], $three['val_one']);
			
			return $common;
		}
	}
	
	/**
	 * 比较单张
	 **/
	protected function single_than($one, $two, $three)
	{
		if($two['val_one'] == $three['val_one'] && $one['val_one'] == $three['val_one']){//三副牌的第一张牌的牌面一致
			if($two['val_two'] == $three['val_two'] && $one['val_two'] == $three['val_two']){//三副牌的第二张牌的牌面一致
				//三副牌的第三张牌的牌面一致(一致用 花色比较  不一致比较大小)
				if($two['val_three'] == $three['val_three'] && $one['val_three'] == $three['val_three']){
					$common = $this->than($one['color'], $two['color'], $three['color']);
					
					return $common;
				} elseif($two['val_three'] == $one['val_three']){//一号牌和二号牌的第三张牌牌面一样
					if($two['val_three'] > $three['val_three']){
						if($two['color'] > $one['color']){
							return 2;
						} else{
							return 1;
						}
					} else{
						return 3;
					}
				} elseif($three['val_three'] == $one['val_three']){//一号牌和三号牌的第三张牌牌面一样
					if($one['val_three'] > $two['val_three']){
						if($three['color'] > $one['color']){
							return 3;
						} else{
							return 1;
						}
					} else{
						return 2;
					}
				} elseif($two['val_three'] == $three['val_three']){//二号牌和三号牌的第三张牌牌面一样
					if($two['val_three'] > $one['val_three']){
						if($two['color'] > $three['color']){
							return 3;
						} else{
							return 2;
						}
					} else{
						return 1;
					}
				} else{//三副牌的第三张拍的牌面均不一致
					$common = $this->than($one['val_three'], $two['val_three'], $three['val_three']);
					
					return $common;
				}
			} elseif($two['val_two'] == $one['val_two']){//一号牌和二号牌的第二张牌牌面一样
				if($two['val_two'] > $three['val_two']){
					if($two['val_three'] == $one['val_three']){
						if($two['color'] > $one['color']){
							return 2;
						} else{
							return 1;
						}
					} else{
						if($two['val_three'] > $one['val_three']){
							return 2;
						} else{
							return 1;
						}
					}
				} else{
					return 3;
				}
			} elseif($three['val_two'] == $one['val_two']){//一号牌和三号牌的第二张牌牌面一样
				if($three['val_two'] > $two['val_two']){
					if($three['val_three'] == $one['val_three']){
						if($three['color'] > $one['color']){
							return 3;
						} else{
							return 1;
						}
					} else{
						if($three['val_three'] > $one['val_three']){
							return 3;
						} else{
							return 1;
						}
					}
				} else{
					return 2;
				}
			} elseif($three['val_two'] == $two['val_two']){//二号牌和三号牌的第二张牌牌面一样
				if($three['val_two'] > $one['val_two']){
					if($three['val_three'] == $two['val_three']){
						if($three['color'] > $two['color']){
							return 3;
						} else{
							return 2;
						}
					} else{
						if($three['val_three'] > $two['val_three']){
							return 3;
						} else{
							return 2;
						}
					}
				} else{
					return 1;
				}
			} else{//三副牌的第二张牌都不一样
				$common = $this->than($one['val_two'], $two['val_two'], $three['val_two']);
				
				return $common;
			}
		} elseif($two['val_one'] == $one['val_one']){//一号牌和二号牌的第一张牌牌面一样
			if($two['val_one'] > $three['val_one']){
				if($two['val_two'] == $one['val_two']){
					if($two['val_three'] == $one['val_three']){
						if($two['color'] > $one['color']){
							return 2;
						} else{
							return 1;
						}
					} else{
						if($two['val_three'] > $one['val_three']){
							return 2;
						} else{
							return 1;
						}
					}
				} else{
					if($two['val_two'] > $one['val_two']){
						return 2;
					} else{
						return 1;
					}
				}
			} else{
				return 3;
			}
		} elseif($three['val_one'] == $one['val_one']){//一号牌和三号牌的第一张牌牌面一样
			if($one['val_one'] > $two['val_one']){
				if($three['val_two'] == $one['val_two']){
					if($three['val_three'] == $one['val_three']){
						if($three['color'] > $one['color']){
							return 3;
						} else{
							return 1;
						}
					} else{
						if($three['val_three'] > $one['val_three']){
							return 3;
						} else{
							return 1;
						}
					}
				} else{
					if($three['val_two'] > $one['val_two']){
						return 3;
					} else{
						return 1;
					}
				}
			} else{
				return 2;
			}
		} elseif($three['val_one'] == $two['val_one']){//二号牌和三号牌的第一张牌牌面一样
			if($two['val_one'] > $one['val_one']){
				if($three['val_two'] == $two['val_two']){
					if($three['val_three'] == $two['val_three']){
						if($three['color'] > $two['color']){
							return 3;
						} else{
							return 2;
						}
					} else{
						if($three['val_three'] > $two['val_three']){
							return 3;
						} else{
							return 2;
						}
					}
				} else{
					if($three['val_two'] > $two['val_two']){
						return 3;
					} else{
						return 2;
					}
				}
			} else{
				return 1;
			}
		} else{
			$common = $this->than($one['val_one'], $two['val_one'], $three['val_one']);
			
			return $common;
		}
	}
	
	
	/* 智勇三张牌面处理 */
	protected function translate($deck)
	{
		$deck_rs = array();
		foreach($deck as $k => $v){
			$carde                = explode('-', $v);
			$deck_rs[$k]['color'] = $carde[0];
			$deck_rs[$k]['brand'] = $carde[1];
			$order[$k]            = $carde[1];
			array_multisort($order, SORT_DESC, $deck_rs);
		}
		
		return $deck_rs;
	}
	
	
	/**
	 * 游戏记录
	 * @desc 用于获取本次直播对应 游戏的中奖情况
	 * @return int code 操作码，0表示成功
	 * @return array info 游戏记录列表
	 * @return string info[][0] 第一个位置中奖情况，0表示输，1表示赢
	 * @return string info[][1] 第二个位置中奖情况，0表示输，1表示赢
	 * @return string info[][2] 第三个位置中奖情况，0表示输，1表示赢
	 * @return string info[][3] 第四个位置中奖情况，0表示输，1表示赢
	 * @return string msg 提示信息
	 */
	public function getGameRecord()
	{
		$action = $this->action;
		$stream = checkNull($this->stream);
		
		$domain = new Domain_Game();
		$list   = $domain->getGameRecord($action, $stream);
		
		return $this->success($list);
	}
	
	/**
	 * 庄家流水
	 * @desc 用于获取庄家流水
	 * @return int code 操作码，0表示成功
	 * @return array info 记录列表
	 * @return string info[].banker_profit 收益
	 * @return string msg 提示信息
	 */
	public function getBankerProfit()
	{
		//$action=$this->action;
		$bankerid = $this->bankerid;
		$action   = 4;
		$stream   = checkNull($this->stream);
		
		$domain = new Domain_Game();
		$list   = $domain->getBankerProfit($bankerid, $action, $stream);
		
		return $this->success($list);
	}
	
	/**
	 * 上庄列表
	 * @desc 用于获取上庄列表
	 * @return int code 操作码，0表示成功
	 * @return array info 列表
	 * @return string info[].id 用户ID
	 * @return string info[].user_nickname 用户ID
	 * @return string info[].avatar 用户ID
	 * @return string info[].coin 用户ID
	 * @return string msg 提示信息
	 */
	protected function getBanker()
	{
		$action = 4;
		$stream = checkNull($this->stream);
		$key    = 'banker_'.$action.'_'.$stream;
		
		$uidlist = array();
		$list    = DI()->redis->hVals($key);
		foreach($list as $v){
			$bankerinfo = json_decode($v, true);
			if($bankerinfo['isout'] == 0){
				$uidlist[] = $bankerinfo;
				$order1[]  = $bankerinfo['addtime'];
			}
		}
		
		array_multisort($order1, SORT_ASC, $uidlist);
		
		$domain    = new Domain_Game();
		$info      = $domain->getBanker($stream);
		$uidlist[] = $info;
		
		return $this->success($uidlist);
	}
	
	/**
	 * 用户上庄
	 * @desc 用于用户上庄
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string info[].coin 账户余额
	 * @return string info[].msg 提示信息
	 * @return string msg 提示信息
	 */
	protected function setBanker()
	{
		$uid     = checkNull($this->uid);
		$token   = checkNull($this->token);
		$stream  = checkNull($this->stream);
		$deposit = checkNull($this->deposit);
		$action  = 4;
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		$key     = 'banker_'.$action.'_'.$stream;
		$isexist = DI()->redis->hGet($key, $uid);
		if($isexist){
			$bankerinfo = json_decode($isexist, 1);
			if($bankerinfo['isout'] == 0){
				return $this->fail('已经申请了!', 1001);
			}
		}
		
		$domain    = new Domain_Game();
		$info      = $domain->setBanker($uid);
		$configpri = getConfigPri();
		$limit     = $configpri['game_banker_limit'];
		if($deposit > $info['coin']){
			return $this->fail('押金超过余额,无法上庄!', 1003);
		}
		if($limit > $deposit){
			return $this->fail('押金不足'.NumberFormat($limit).',无法上庄', 1002);
		}
		
		$info['coin']    = NumberFormat($deposit);
		$info['deposit'] = $deposit;
		$info['isout']   = 0;
		$info['addtime'] = time();
		
		DI()->redis->hSet($key, $uid, json_encode($info));
		
		$userinfo        = $domain->setDeposit($uid, $deposit);
		$info[0]['coin'] = (string)$userinfo['coin'];
		$info[0]['msg']  = '申请成功';
		
		return $this->success($info);
	}
	
	/**
	 * 用户下庄
	 * @desc 用于用户上庄
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string info[].msg 提示信息
	 * @return string msg 提示信息
	 */
	protected function quietBanker()
	{
		$uid    = checkNull($this->uid);
		$stream = checkNull($this->stream);
		$action = 4;
		$key    = 'banker_'.$action.'_'.$stream;
		
		$isexist = DI()->redis->hGet($key, $uid);
		if($isexist){
			$banker = json_decode($isexist, true);
			
			$banker['isout'] = 1;
			DI()->redis->hSet($key, $uid, json_encode($banker));
		}
		
		return $this->success([0 => ['msg' => '下庄成功!']]);
	}
	
}

